﻿using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace LD
{
    internal class SerializableHelper
    {
        /// <summary>
        /// 序列化
        /// </summary>
        public static void Serialize(object obj, string path)
        {
            using (Stream st = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(st, obj);
            }
        }
        /// <summary>
        /// 反序列化
        /// </summary>
        public static object DeSerialize(string path)
        {
            if (!File.Exists(path))
            {
                return null;
            }
            using (Stream st = new FileStream(path, FileMode.Open))
            {
                BinaryFormatter bf = new BinaryFormatter();
                return bf.Deserialize(st);
            }
        }
    }
}
